#!/usr/bin/python
# -*- coding: utf-8 -*-

import os, time, glob
from logger import Logger
import globals

totalDuration = 0

def formatDuration(duration):
    duration = int(duration)
    hours = duration / 3600
    minutes = (duration - hours * 3600) / 60
    seconds = duration % 60
    if hours > 0:
        return "%dh%dm%ds" % (hours, minutes, seconds)
    elif minutes > 0:
        return "%dm%ds" % (minutes, seconds)
    else:
        return "%ds" % (seconds)

def execute(command):
    print "Executing:", command
    startTime = time.time()
    os.system(command)
    duration = round(time.time() - startTime)
    globals.totalDuration = globals.totalDuration + duration
    logger = Logger()
    logger.log('\n%s\n#%s\n'%(command, formatDuration(duration)))

def removeFile(fname):
    try:
        print "Removing", fname
        os.remove(fname)
        logger = Logger()
        logger.log('rm "%s" \n' % (fname))
    except:
        print "Unable to remove", fname

def expandWildcards(arguments):
    allFiles = []
    allFiles.extend([arg for arg in arguments if os.path.exists(arg)])
    [allFiles.extend(element) for element in [glob.glob(arg) for arg in arguments if not os.path.exists(arg)]]
    return list(set(allFiles))
